package com.bodgittandscarper.brokerhmi.view;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import com.bodgittandscarper.broker.types.DatabaseSchema;
import com.bodgittandscarper.brokerhmi.control.BASControl;
import com.bodgittandscarper.brokerhmi.model.HMIModel;

/**
 * Main view class. The GUI is split up into panels containing different GUI areas.    
 * 
 * @author Morten Rummelhoff
 * @see TablePanel
 * @see BookPanel
 * @see SearchPanel
 * @see BottomStatusPanel
 */
public class HMIMainView {

	private Logger log = Logger.getLogger(getClass().getName());

	private HMIModel hmiModel;
	private BASControl basControl;
	private DatabaseSchema databaseScheme;

	private RecordTableModel tableModel;

	private BottomStatusPanel bottomStatusPanel;

	/**
	 * Sole constructor for view main class. 
	 * 
	 * @param hmiModel for supporting propertyChange and notification between different view classes.
	 * @param control support for interactions with the database.
	 */
	public HMIMainView(HMIModel hmiModel, BASControl control) {
		this.hmiModel = hmiModel;
		this.basControl = control;

		this.databaseScheme = control.getDatabaseScheme();

		createGUI();
	}

	private void createGUI() {
		JFrame frame = new JFrame("Bodgitt and Scarper HMI (Mode: " + basControl.getMode() + ")");

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		   

		//setup Menu
		frame.setJMenuBar(createMenuBar());

		frame.getContentPane().add(createMainPanel());
		frame.pack();
		frame.setSize(800, 760);

		//center frame according to screen
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}

	private JPanel createMainPanel() {
		JPanel mainPanel = new JPanel(new BorderLayout());
		mainPanel.setBorder(BorderFactory.createTitledBorder("Bodgitt and Scarpers"));

		JPanel gridbagLayoutPanel = new JPanel();
		gridbagLayoutPanel.setLayout(new GridBagLayout());

		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 0.2;
		c.weighty = 0.2;
		tableModel = new RecordTableModel(this.databaseScheme);
		TablePanel tablePanel = new TablePanel(hmiModel, basControl, tableModel);
		gridbagLayoutPanel.add(tablePanel.getPresentation(), c);

		JPanel horizontalPanel = new JPanel();
		horizontalPanel.setLayout(new BoxLayout(horizontalPanel, BoxLayout.X_AXIS));

		BookPanel bookPanel = new BookPanel(hmiModel, basControl);
		horizontalPanel.add(bookPanel.getPresentation());

		SearchPanel searchPanel = new SearchPanel(hmiModel, basControl, tableModel);
		horizontalPanel.add(searchPanel.getPresentation());

		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridy = 1;
		c.weighty = 0;
		gridbagLayoutPanel.add(horizontalPanel, c);

		mainPanel.add(gridbagLayoutPanel, BorderLayout.CENTER);

		bottomStatusPanel = new BottomStatusPanel(hmiModel, basControl);
		mainPanel.add(bottomStatusPanel.getPresentation(), BorderLayout.SOUTH);

		return mainPanel;
	}

	private JMenuBar createMenuBar() {
		JMenuBar menuBar = new JMenuBar();

		JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic(KeyEvent.VK_F);

		fileMenu.addSeparator();
		JMenuItem exitMenu = new JMenuItem("Exit");
		exitMenu.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);

			}
		});
		fileMenu.add(exitMenu);

		menuBar.add(fileMenu);

		JMenu helpMenu = new JMenu("Help");
		helpMenu.setMnemonic(KeyEvent.VK_H);
		JMenuItem helpMenuItem = new JMenuItem("Help Content");
		helpMenuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (log.isLoggable(Level.INFO))
					log.log(Level.INFO, "See userguide.html");

				if (Desktop.isDesktopSupported() && new File("docs/userguide.html").exists()) {
					try {
						Desktop.getDesktop().open(new File("docs/userguide.html"));
					} catch (IOException e) {
						if (log.isLoggable(Level.INFO))
							log.log(Level.INFO, "", e);

					}
				}

			}
		});

		helpMenu.add(helpMenuItem);
		menuBar.add(helpMenu);

		return menuBar;
	}
}
